package com.quickask.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.quickask.entity.AsknowUserEntity;
import com.quickask.entity.AsknowUserLocationEntity;
import com.quickask.entity.AsknowUserPrivilegeEntity;
import com.quickask.operate.data.ResultCode;
import com.quickask.service.UserService;
import org.json.JSONObject;

import java.util.*;

/**
 * Created by NaSa on 2015/9/3.
 */
public class UserAction extends ActionSupport {

    private UserService userService;
    private Map<String, Object> responseResult = new LinkedHashMap<String, Object>();
    private Integer userId;  //用户Id
    private String accountNo; //账号
    private String passwordNo; //密码
    private Integer type;  //账号类型，0为手机号，1为普通账号
    private String zone;  //区号，中国默认86
    private String code;  //验证码
    private String token;  //refreshToken
    private Byte sex;  //性别
    private String nickname;  //昵称
    private Integer age;  //年龄
    private String userJson;


    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String execute() {
        responseResult.put("data", userService.showAllUser());
        responseResult.put("code", "10001");
        responseResult.put("msg", "登录成功");
        responseResult.put("result", "success");
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String date = sdf.format(System.currentTimeMillis());
//        Timestamp time = Timestamp.valueOf(date);
//        System.out.println(userService.showAllUser().get(0).toString());
//
//        Map<String, Object> outerMap = new HashMap<String, Object>();
//        outerMap.put("data",list);
//        outerMap.put("code",10000);
//        outerMap.put("result","success");
//        this.setResponseResult(outerMap);
        return SUCCESS;
    }


    // http://moonlight2015.xicp.net/QuickAsk/user/register.do?accountNo=Hackway&passwordNo=jljth30bmba3jsfoie&zone=86&code=3334&type=?
    public String register() {
        AsknowUserEntity asknowUserEntity = null;
        List<AsknowUserPrivilegeEntity> asknowUserPrivilegeEntities = null;
        ActionContext actionContext = ActionContext.getContext();
        Map session = actionContext.getSession();
        int resultCode;
        if (getType() == 0) {
            resultCode = userService.registerByPhone(getAccountNo(), getPasswordNo(), getZone(), getCode()); //手机注册，返回注册的结果码
            if (resultCode == ResultCode.SMS_200) {//验证成功
                asknowUserEntity = userService.loginByPhone(getAccountNo(),getPasswordNo());
                asknowUserPrivilegeEntities = new ArrayList<AsknowUserPrivilegeEntity>();
                asknowUserPrivilegeEntities.add(asknowUserEntity.getAsknowUserPrivilegeEntity());
                session.put(String.valueOf(asknowUserEntity.getUserId()), asknowUserEntity.getUserToken());  //设置session
                responseResult.put("data", asknowUserPrivilegeEntities);
                responseResult.put("token",asknowUserEntity.getUserToken());
                responseResult.put("flag", true);
                responseResult.put("code", ResultCode.SUCCESS);
                responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
            } else {//验证失败
                responseResult.put("data", null);
                responseResult.put("token",null);
                responseResult.put("flag", false);
                responseResult.put("code", resultCode);
                responseResult.put("msg", ResultCode.getErrmsg(resultCode));
            }
        } else if (getType() == 1) {
            resultCode = userService.registerByNormal(getAccountNo(), getPasswordNo()); //普通注册，返回注册的结果码
            if (resultCode == ResultCode.SUCCESS) {
                asknowUserEntity = userService.loginByNormal(getAccountNo(), getPasswordNo());
                asknowUserPrivilegeEntities = new ArrayList<AsknowUserPrivilegeEntity>();
                asknowUserPrivilegeEntities.add(asknowUserEntity.getAsknowUserPrivilegeEntity());
                session.put(String.valueOf(asknowUserEntity.getUserId()), asknowUserEntity.getUserToken());  //设置session
                responseResult.put("data", asknowUserPrivilegeEntities);
                responseResult.put("token",asknowUserEntity.getUserToken());
                responseResult.put("flag", true);
                responseResult.put("code", resultCode);
                responseResult.put("msg", ResultCode.getErrmsg(resultCode));
            } else {
                responseResult.put("data", null);
                responseResult.put("token",null);
                responseResult.put("flag", false);
                responseResult.put("code", resultCode);
                responseResult.put("msg", ResultCode.getErrmsg(resultCode));
            }
        }
        return SUCCESS;
    }

    //  http://moonlight2015.xicp.net/QuickAsk/user/login.do?accountNo=Hackway&passwordNo=jljth30bmba3jsfoie&type=1
    public String login() {
        //AsknowUserPrivilegeEntity asknowUserPrivilegeEntity = userService.loginByPhone("13560411384","324rwer3");
        AsknowUserEntity asknowUserEntity = null;
        List<AsknowUserPrivilegeEntity> asknowUserPrivilegeEntities = null;
        ActionContext actionContext = ActionContext.getContext();
        Map session = actionContext.getSession();
        if (getType() == 0) {
            asknowUserEntity = userService.loginByPhone(getAccountNo(), getPasswordNo());
        } else if (getType() == 1) {
            asknowUserEntity = userService.loginByNormal(getAccountNo(), getPasswordNo());
        }
        if (asknowUserEntity == null) {
            responseResult.put("data", null);
            responseResult.put("token",null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.USERNICKNAME_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.USERNICKNAME_ERROR));
        } else {
            session.put(String.valueOf(asknowUserEntity.getUserId()), asknowUserEntity.getUserToken());  //设置session
            asknowUserPrivilegeEntities = new ArrayList<AsknowUserPrivilegeEntity>();
            asknowUserPrivilegeEntities.add(asknowUserEntity.getAsknowUserPrivilegeEntity());
            responseResult.put("data", asknowUserPrivilegeEntities);
            responseResult.put("token",asknowUserEntity.getUserToken());
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        }
        return SUCCESS;
    }

    //  http://moonlight2015.xicp.net/QuickAsk/user/logout.do?userId=1
    public String logout() {
        int resultCode = userService.logout(getUserId());
        responseResult.put("data", null);
        if (resultCode == ResultCode.USERID_ERROR) {
            responseResult.put("flag",false);
            responseResult.put("code", ResultCode.USERID_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.USERID_ERROR));
        } else {
            responseResult.put("flag",true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        }
        return SUCCESS;
    }


    //  http://moonlight2015.xicp.net/QuickAsk/user/requestRefreshToken.do?userId=1&token=1065365FD81C56766911D88602F65F4CBABC9BD1
    public String requestRefreshToken() {
        int resultCode = userService.updateRefreshToken(getUserId(), getToken());
        AsknowUserPrivilegeEntity asknowUserPrivilegeEntity;
        if(resultCode == getUserId()) {
            asknowUserPrivilegeEntity = userService.loadAsknowUserPrivilegeEntity(resultCode);
            responseResult.put("data",asknowUserPrivilegeEntity);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else if(resultCode == ResultCode.USER_TOKEN_ERROR) {
            responseResult.put("data",null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.USER_TOKEN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.USER_TOKEN_ERROR));
        } else if(resultCode == ResultCode.USERID_ERROR) {
            responseResult.put("data",null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.USERID_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.USERID_ERROR));
        } else {
            responseResult.put("data",null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }
        return SUCCESS;
    }

    //  http://moonlight2015.xicp.net/QuickAsk/user/updateUserImformation.do?userId=2&age=21&sex=1&nickname=Hackway
    public String updateUserImformation() {
        Map<String, Object> imformationMap = new HashMap<String, Object>();
        if(null!=getNickname() && !"".equals(getNickname())) {
            imformationMap.put("userNickname",getNickname());
        }
        if(null!=getAge()) {
            imformationMap.put("userAge",getAge());
        }
        if(null!=getSex()) {
            imformationMap.put("userSex",getSex());
        }
        int resultCode = userService.updatePersonalImformation(getUserId(), imformationMap);
        switch (resultCode) {
            case ResultCode.USERID_ERROR:
                responseResult.put("data",null);
                responseResult.put("flag", false);
                responseResult.put("code", ResultCode.USERID_ERROR);
                responseResult.put("msg", ResultCode.getErrmsg(ResultCode.USERID_ERROR));break;
            case ResultCode.UNKNOWN_ERROR:
                responseResult.put("data",null);
                responseResult.put("flag", false);
                responseResult.put("code", ResultCode.UNKNOWN_ERROR);
                responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));break;
            case ResultCode.SUCCESS:
                responseResult.put("data",userService.loadAsknowUserPrivilegeEntity(getUserId()));
                responseResult.put("flag", true);
                responseResult.put("code", ResultCode.SUCCESS);
                responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));break;
        }
        return SUCCESS;
    }

    //  http://moonlight2015.xicp.net/QuickAsk/user/generateUploadToken.do
    public String generateUploadToken() {
        try{
            responseResult.put("qiniuToken",userService.generateQiniuToken());
        }catch (Exception e) {
            responseResult.put("qiniuToken",null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.QINIU_TOKEN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.QINIU_TOKEN_ERROR));
            return SUCCESS;
        }
        responseResult.put("flag", true);
        responseResult.put("code", ResultCode.SUCCESS);
        responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        return SUCCESS;
    }

    //http://moonlight2015.xicp.net/QuickAsk/user/updateLocation.do
    //userJson: {"userId": 3,"altitude":0.56,"longitude":108.376345,"latitude":22.816963,"provinceCode":"44","cityCode":"4401","blockCode":"440113","locationDetail": "火星殖民地13区第五大街","type":1}
    public String updateLocation() {
        if (userJson == null) {
            responseResult.put("data", null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.PARAMS_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.PARAMS_ERROR));
            return SUCCESS;
        }
        AsknowUserLocationEntity asknowUserLocationEntity = userService.updateUserLocation(userJson);
        if(asknowUserLocationEntity != null) {
            responseResult.put("data",asknowUserLocationEntity);
            responseResult.put("flag", true);
            responseResult.put("code", ResultCode.SUCCESS);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.SUCCESS));
        } else {
            responseResult.put("data",null);
            responseResult.put("flag", false);
            responseResult.put("code", ResultCode.UNKNOWN_ERROR);
            responseResult.put("msg", ResultCode.getErrmsg(ResultCode.UNKNOWN_ERROR));
        }

        return SUCCESS;
    }

    public Map getResponseResult() {
        return responseResult;
    }

    public void setResponseResult(Map responseResult) {
        this.responseResult = responseResult;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getPasswordNo() {
        return passwordNo;
    }

    public void setPasswordNo(String passwordNo) {
        this.passwordNo = passwordNo;
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public String getZone() {
        return zone;
    }

    public void setZone(String zone) {
        this.zone = zone;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Byte getSex() {
        return sex;
    }

    public void setSex(Byte sex) {
        this.sex = sex;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getUserJson() {
        return userJson;
    }

    public void setUserJson(String userJson) {
        this.userJson = userJson;
    }

}
